package com.plant.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.plant.common.domain.User;
import com.plant.common.result.Result;
import com.plant.user.domain.VO;
import com.plant.user.service.UserService;
import com.plant.user.utils.SmsUtils;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    //登录
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User us = userService.login(user.getPhone(), user.getPassword());
        return Result.ok(us);
    }

    //查询
    @PostMapping("findAll")
    public Result findAll() {
        List<User> list = userService.findAll();
        return Result.ok(list);
    }

    //充值
    @PostMapping("/recharge")
    public Result recharge(@RequestBody User user) {
        System.out.println(user);
        int i = userService.recharge(user);
        System.out.println(i);
        return Result.ok();
    }

    //提现
    @PostMapping("/withdrawal")
    public Result withdrawal(@RequestBody User user) {
        log.info("i:{}", user);
        int i = userService.withdrawal(user);
        log.info("i:{}", i);
        return Result.ok();
    }


    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        //111
        User user = userService.findById(id);
        return Result.ok(user);
    }

    @GetMapping("/findById1/{id}")
    public User findById1(@PathVariable Integer id) {
        User user = userService.findById(id);
        return user;
    }

    //注册
    @GetMapping("/register/{phone}")
    public Result register(@PathVariable("phone") String phone) {
        //生成随机验证码
        String code = RandomUtil.randomNumbers(6);
        try {
            SmsUtils.sendSms(phone, code);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //向redis中存一份
        redisTemplate.opsForValue().set(phone, code, 3, TimeUnit.MINUTES);
        return Result.ok(code);
    }


    //添加用户
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }


    @PostMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody User user) {
        HashMap<String, Object> map = new HashMap<>();
        //空值判断p
        if (user.getPhone() == null) {
            map.put("phone", "手机号不能为空");
        }
        //手机号是否正确
        if (!StringUtil.isNullOrEmpty(user.getPhone())) {
            String code = redisTemplate.opsForValue().get(user.getPhone()).toString();
            if (!code.equals(user.getPhone())) {
                map.put("code", "手机号错误·");
            }
        }
        return Result.ok();
    }

    //修改密码
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody VO vo) {
        return userService.updateUser(vo);
    }

    //根据手机号查询
    @GetMapping("/chaxun/{phone}")
    public Result chaxun(@PathVariable("phone") String phone) {
        User user = userService.chaxun(phone);
        return Result.ok(user);
    }


    //发送验证码
    @PostMapping("/fsCode")
    public Result fsCode(@RequestBody User user) {
        String code = userService.fsCode(user);
        return Result.ok(code);
    }


    //我的设置的修改
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.ok();
    }


    @GetMapping("/updateUserType")
    public int updateUserType(@RequestParam("id") Integer id) {
        return userService.updateUserType(id);
    }

  @GetMapping("/SignOut/{phone}")
    public Result SignOut(@PathVariable("phone") String phone){
        return Result.ok(userService.SignOut(phone))  ;
    }


}

